package com.vg.leftmenu;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;

import com.androidhive.jsonparsing.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class ChangeAvatarActivity extends Activity {
	private Cursor cursor;
	private int columnIndex;
	String imagePath;
	private byte[] by;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_avatar);

		String[] projection = { MediaStore.Images.Thumbnails._ID };
		cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
				projection,	null, null, MediaStore.Images.Thumbnails.IMAGE_ID);
		columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

		GridView sdcardImages = (GridView) findViewById(R.id.change_avatar_gridview);
		sdcardImages.setAdapter(new ImageAdapter(this));

		sdcardImages.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView parent, View v, int position,
					long id) {
				// Get the data location of the image
				String[] projection = { MediaStore.Images.Media.DATA };
				cursor = managedQuery(
						MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
						projection, null, null, null);
				columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
				cursor.moveToPosition(position);
				imagePath = cursor.getString(columnIndex);
				Toast.makeText(getBaseContext(), "Ban chon anh " + imagePath, Toast.LENGTH_LONG).show();
				Bitmap bm = BitmapFactory.decodeFile(imagePath);
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				bm.compress(CompressFormat.PNG, 0, stream);
				by = stream.toByteArray();
			}
		});
		
		Button btnSelect = (Button) findViewById(R.id.change_avatar_btnOk);
		btnSelect.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(by!=null){
					Toast.makeText(getBaseContext(), by.toString(), Toast.LENGTH_LONG).show();
					finish();
				}
				else Toast.makeText(getBaseContext(), "Ban chua chon anh", Toast.LENGTH_LONG).show();
			}
		});
	}

	public class ImageAdapter extends BaseAdapter {
		private Context context;

		public ImageAdapter(Context c) {
			context = c;
		}

		public int getCount() {
			return cursor.getCount();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView picturesView;
			if (convertView == null) {
				picturesView = new ImageView(context);
				// Move cursor to current position
				cursor.moveToPosition(position);
				// Get the current value for the requested column
				int imageID = cursor.getInt(columnIndex);
				// Set the content of the image based on the provided URI
				picturesView.setImageURI(Uri.withAppendedPath(
						MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+ imageID));
				picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
				picturesView.setPadding(2, 2, 2, 2);
				picturesView.setLayoutParams(new GridView.LayoutParams(120, 120));
			} else {
				picturesView = (ImageView) convertView;
			}
			return picturesView;
		}
	}
}